package org.madgecko.api.controller;

import org.madgecko.basic.model.AjaxReturn;
import org.madgecko.basic.model.Student;
import org.madgecko.basic.service.iface.IStudentService;
import org.madgecko.basic.util.SecurityUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;

/**
 * Author       : MadFrog
 * Date         : 2018/11/25 11:52
 * Description  : 学生登录控制器
 */
@Controller
public class StudentLoginController {

    @Inject
    private IStudentService studentService;

    @ResponseBody
    @RequestMapping(value = "/student/login", produces={"application/json; charset=UTF-8"})
    public AjaxReturn login(String studentNumber, String password){
        try {
            Student student = studentService.loadByStuNum(studentNumber);
            if (student!=null){
                if (SecurityUtil.md5(password).equals(student.getPassword())){
                    return new AjaxReturn(true, "登录成功");
                }else {
                    return new AjaxReturn(false, "密码错误");
                }
            }else {
                return new AjaxReturn(false, "学号错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxReturn(false);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/student/updatePassword", produces={"application/json; charset=UTF-8"})
    public AjaxReturn updatePassword(String studentNumber, String password){
        try {
            Student student = studentService.loadByStuNum(studentNumber);
            if (student!=null){
                student.setPassword(SecurityUtil.md5(password));
                studentService.update(student);
                return new AjaxReturn(true, "修改密码成功");
            }else {
                return new AjaxReturn(false, "学号错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxReturn(false);
        }
    }

}
